#!/usr/bin/python3
# coding=utf-8
import sys
import json


class CpingDeamon(object):
    def __init__(self, f):
        super(CpingDeamon, self).__init__()
        self._file = f

    def proc(self):
        postprocess_result = {
            "code": 0,
            "err_msg": "",
            "result": {}
        }
        dPing = {"stat": None, "seq": []}
        for l in self._file.readlines():
            obj = json.loads(l)
            if "stat" in obj:
                dPing['stat'] = obj["stat"]
            else:
                dPing["seq"].append(obj)

        ret = {}
        ret['pingtraceFlow'] = {}
        ret["pingtraceFlow"]['data'] = []
        for stage in dPing['stat']['stage']:
            ret["pingtraceFlow"]['data'].append(
                {'key': stage['delay'], 'title': stage['delay'], 'text': "Max:{} Min:{} Avg:{}".format(
                    stage['max'], stage['min'], stage['avg'])}
            )
        postprocess_result['result'] = ret
        s = json.dumps(postprocess_result, indent=4)
        print(s)


if __name__ == "__main__":
    with open(sys.argv[1], "r") as f:
        c = CpingDeamon(f)
        c.proc()
